package com.vhall.component.service.chat.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vhall.component.dao.chat.QuickRepliesMapper;
import com.vhall.component.entity.chat.dto.QuickRepliesDTO;
import com.vhall.component.entity.chat.entity.QuickRepliesEntity;
import com.vhall.component.entity.chat.enums.QuickRepliesStatusEnum;
import com.vhall.component.entity.chat.vo.QuickRepliesClientListVO;
import com.vhall.component.service.chat.QuickRepliesClientService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * QuickRepliesClientServiceImpl-
 *
 * @author xing.wang
 * @date 2022/9/9
 */
@Service("QuickRepliesClient")
@Slf4j
@RequiredArgsConstructor
public class QuickRepliesClientServiceImpl extends ServiceImpl<QuickRepliesMapper, QuickRepliesEntity> implements QuickRepliesClientService {
    private final QuickRepliesMapper quickRepliesMapper;
    /**
     * 最大启用限制数量
     */
    @Value("${project.limit.quick-replies.open-num:10}")
    private Integer OPEN_COUNT_LIMIT;

    @Override
    public List<QuickRepliesDTO> list(QuickRepliesClientListVO vo) {


        LambdaQueryWrapper<QuickRepliesEntity> where = Wrappers.lambdaQuery();

        where.eq(QuickRepliesEntity::getStatus, QuickRepliesStatusEnum.OPEN.getStatus());
        where.isNull(QuickRepliesEntity::getDeletedAt);
//        where.eq(QuickRepliesEntity::getAccountId, masterAccountId).or()
//                .eq(QuickRepliesEntity::getType, QuickRepliesTypeEnum.SYSTEM.getType());

        where.orderByDesc(QuickRepliesEntity::getCreatedAt);
        where.last("limit " + OPEN_COUNT_LIMIT);
        List<QuickRepliesEntity> quickRepliesEntities = quickRepliesMapper.selectList(where);
        return quickRepliesEntities.stream().map(temp -> {
            QuickRepliesDTO data = new QuickRepliesDTO();
            BeanUtil.copyProperties(temp, data, false);
            return data;
        }).collect(Collectors.toList());
    }
}
